﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

namespace Groove.Winform.Component
{
    public partial class ControlButton : Control
    {
        private MouseState _mouseState = MouseState.Out;

        private Color _hoverBackColor = Color.Red;
        public Color HoverBackColor
        {
            get => _hoverBackColor;
            set
            {
                _hoverBackColor = value;
                Invalidate();
            }
        }

        private Color _iconColor = Color.White;
        public Color IconColor
        {
            get => _iconColor;
            set
            {
                _iconColor = value;
                Invalidate();
            }
        }

        private Color _iconHoverColor = Color.Blue;
        public Color IconHoverColor
        {
            get => _iconHoverColor;
            set
            {
                _iconHoverColor = value;
                Invalidate();
            }
        }

        private ControlButtonType _ctlBtnType = ControlButtonType.Minimum;
        public ControlButtonType ControlButtonType
        {
            get => _ctlBtnType;
            set
            {
                _ctlBtnType = value;
                Invalidate();
            }
        }

        public ControlButton()
        {
            DoubleBuffered = true;
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
            MouseEnter += (s, e) =>
            {
                _mouseState = MouseState.Hover;
                Invalidate();
            };
            MouseLeave += (s, e) =>
            {
                _mouseState = MouseState.Out;
                Invalidate();
            };
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            var g = pe.Graphics;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;

            //Draw background
            var backColor = _mouseState == MouseState.Hover ? HoverBackColor : Parent.BackColor;
            g.Clear(backColor);

            //Draw icon
            var iconColor= _mouseState == MouseState.Hover ? IconHoverColor : IconColor;
            switch (ControlButtonType)
            {
                case ControlButtonType.Close:
                    var leftTop = new Point(Padding.Left, Padding.Top);
                    var rightBottom = new Point(Width - Padding.Right, Height - Padding.Bottom);
                    var leftBottom = new Point(Padding.Left, Height - Padding.Bottom);
                    var rightTop = new Point(Width - Padding.Right, Padding.Top);
                    using(var pen=new Pen(iconColor))
                    {
                        g.DrawLine(pen, leftTop, rightBottom);
                        g.DrawLine(pen, leftBottom, rightTop);
                    }
                    break;
                case ControlButtonType.Minimum:
                    var left = new Point(Padding.Left, Height / 2);
                    var right = new Point(Width - Padding.Right, Height / 2);
                    using (var pen = new Pen(iconColor))
                    {
                        g.DrawLine(pen, left, right);
                    }
                    break;
                case ControlButtonType.Maximum:
                default:
                    break;
            }
        }
    }
}
